<template>
  <div class="pages_content table business">
    <el-container>
      <el-header>
        <breaDcrumb />
      </el-header>
      <el-main>
        <div class="header_right" @click="showCardNav = !showCardNav">
          <div class="title">搜索</div>
          <div>
            <div :class="{ 'arrowTransform': !showCardNav, 'arrowTransformReturn': showCardNav}">
              <i style="margin-right: 5px" class="el-icon-arrow-down"></i>
            </div>
          </div>
        </div>
        <div v-show="showCardNav" class="hr"></div>
        <el-collapse-transition>
          <div v-show="showCardNav">
            <div class="search">
              <span style="display: inline-block;margin-bottom: 10px;">
                <span style="margin-right: 12px;">系统名称</span>
                <el-input v-model="tableParam.name" style="width: 200px;" clearable placeholder="请输入" @keyup.enter.native="queryPage()"/>
              </span>
              <span style="margin-left: 24px;">
                <span style="margin-right: 12px;">编码</span>
                <el-input v-model="tableParam.code" style="width: 20%;max-width: 200px;" clearable placeholder="请输入" @keyup.enter.native="queryPage()" />
              </span>
              <el-button icon="el-icon-search" size="medium" class="bottom_all but1" @click="(tableParam.page = 1), queryPage()">查 询</el-button>
              <el-button size="medium" class="bottom_all_two" @click="reset()">重 置</el-button>
            </div>
          </div>
        </el-collapse-transition>
        <div class="hr"></div>
        <div class="card">
          <div class="card_nav">
            <div>
              <el-button v-if="ButtonList.findIndex(res=>(res.buttonValue=='TIANJIA'))!==-1" size="small" class="bottom_all" style="margin-left: 0;" @click="add()">
                <i class="icon iconfont iconicon_button_add" style="font-size: 12px;"></i>
                添加
              </el-button>
              <el-button v-if="ButtonList.findIndex(res=>(res.buttonValue=='SHANCHU'))!==-1" size="small" class="bottom_two" @click="batchUpdateOrDelete('update_status', 0)">
                <i class="icon iconfont iconicon_button_delete" style="font-size: 12px;"></i>
                删除
              </el-button>
              <el-button v-if="ButtonList.findIndex(res=>(res.buttonValue=='RIYONG'))!==-1" size="small" class="bottom_three" @click="batchUpdateOrDelete('update_enable', 1)">
                <i class="icon iconfont iconicon_button_startuse" style="font-size: 12px;"></i>
                启用
              </el-button>
              <el-button v-if="ButtonList.findIndex(res=>(res.buttonValue=='JINYONG'))!==-1" size="small" class="bottom_four" @click="batchUpdateOrDelete('update_enable', 0)">
                <i class="icon iconfont iconicon_button_disable1" style="font-size: 12px;"></i>
                禁用
              </el-button>
            </div>
            <div>
              <el-link v-if="ButtonList.findIndex(res=>(res.buttonValue=='BAOCUNPXH'))!==-1" :underline="false" @click="updateSortNum()">
                <span class="el-icon--left">
                  <i class="iconfont iconicon_button_sort1"></i>
                </span>
                保存排序号
              </el-link>
              <el-dropdown :hide-on-click="false" trigger="click" style="margin: 0 20px;">
                <el-link :underline="false">
                  <span class="el-icon--left">
                    <i class="iconfont iconicon_button_filtrate"></i>
                  </span>
                  筛选
                </el-link>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item v-for="(item, index) in table" :key="index">
                    <el-checkbox v-model="item.status">{{ item.label }}</el-checkbox>
                  </el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
              <el-link v-if="ButtonList.findIndex(res=>(res.buttonValue=='DAOCHU'))!==-1" :underline="false" @click="$Download(DownloadURL, tableParam);">
                <span class="el-icon--left">
                  <i class="iconfont iconicon_button_export1"></i>
                </span>
                导出
              </el-link>
            </div>
          </div>
          <div class="card_info" :style="style">
            <el-table ref="multipleTable" :header-cell-style="{ background: '#f3f6f9' }" :cell-style="{ borderRight: 'none' }" border stripe style="width:100%" :data="tableData" tooltip-effect="dark" @selection-change="handleSelectionChange">
              <el-table-column align="center" type="selection" width="50"></el-table-column>
              <template v-for="(item, index) in table">
                <el-table-column v-if="item.status" :key="index" :label="item.label" align="center" :width="item.width" :fixed=" item.label =='操作'?'right':false">
                  <template slot-scope="scope">
                    <span v-if="scope.row.enable == 0 && item.label == '状态'" style="color:#969799;">禁用</span>
                    <span v-if="scope.row.enable == 1 && item.label == '状态'" style="color:#00A754;">启用</span>
                    <el-button v-if="item.label == '操作'&&ButtonList.findIndex(res=>(res.buttonValue=='XIUGAI'))!==-1" class="bottom_one" size="mini" @click="update(scope.row)">
                      <i class="icon iconfont iconicon_button_modification" style="font-size: 12px;"></i>
                      修改
                    </el-button>
                    <div v-if="item.label == '排序号'">
                      <el-input v-model="scope.row.sortNum"></el-input>
                    </div>
                    <span v-if="item.label !== '排序号' && item.label !== '操作' && item.label !== '状态'">{{ scope.row[item.prop] }}</span>
                  </template>
                </el-table-column>
              </template>
            </el-table>
          </div>
        </div>
      </el-main>
      <div class="card_bottom">
        <div class="card_bottom_left">
          <div class="top_page_totle">总共{{ total }}条</div>
          <el-pagination :current-page.sync="tableParam.page" :page-size="tableParam.pageSize" :page-sizes="[10, 20, 30, 40,50,100,200,500,1000]" layout="prev,pager,sizes,next,jumper" :total="total" @size-change="sizeChange" @current-change="queryPage()"></el-pagination>
        </div>
        <div>
          <el-button size="mini" icon="el-icon-refresh-right" class="bottom_all_two" @click="queryPage()">刷新</el-button>
        </div>
      </div>
    </el-container>
    <!-- 添加 -->
    <el-dialog v-dialogDrag :visible.sync="dialogVisible" width="700px" :close-on-click-modal="false" :before-close="resefrom">
      <span slot="title" class="dialog-title">{{ dialogtitle }}</span>
      <div class="dialoginfo">
        <el-form ref="dialogForm" :rules="rules" :inline="true" :model="from" label-width="100px">
          <el-form-item prop="name">
            <span slot="label" style="color:  #FF0000 ;">名称</span>
            <el-input v-model="from.name" placeholder="请输入" clearable></el-input>
          </el-form-item>
          <el-form-item prop="code">
            <span slot="label" style="color:  #FF0000 ;">编码</span>
            <el-input v-model="from.code" placeholder="请输入" clearable @input="changecode('code')"></el-input>
          </el-form-item>
          <el-form-item prop="userId">
            <span slot="label">系统管理员</span>
            <div style="width:200px;cursor: pointer;" @click="handleClick">{{from.userName}}</div>
          </el-form-item>
          <el-form-item prop="integrateWayId">
            <span slot="label">集成方式</span>
            <el-select v-model="from.integrateWayId" placeholder="请选择">
              <el-option v-for="item in systemintegrationmode" :key="item.id" :label="item.itemName" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="ssoAccount">
            <span slot="label">SSO账号</span>
            <el-input v-model="from.ssoAccount" placeholder="请输入" clearable @input="changecode('ssoAccount')" />
          </el-form-item>
          <el-form-item prop="ssoPassword">
            <span slot="label">SSO密码</span>
            <el-input v-model="from.ssoPassword" placeholder="请输入" clearable @input="changecode('ssoPassword')" />
          </el-form-item>
          <el-form-item prop="path">
            <span slot="label">入口pash</span>
            <el-input v-model="from.path" placeholder="请输入" clearable />
          </el-form-item>
          <el-form-item prop="typeId">
            <span slot="label">类型</span>
            <el-select v-model="from.typeId" placeholder="请选择">
              <el-option v-for="item in systemtype" :key="item.id" :label="item.itemName" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="entrance">
            <span slot="label">系统入口</span>
            <el-input v-model="from.entrance" placeholder="请输入" clearable />
          </el-form-item>
          <el-form-item prop="sortNum">
            <span slot="label">排序号</span>
            <el-input v-model="from.sortNum" placeholder="请输入" type="Number" clearable />
          </el-form-item>
          <el-form-item label="状态" prop="enable">
            <el-radio-group v-model="from.enable">
              <el-radio :label="1">启用</el-radio>
              <el-radio :label="0">禁用</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="系统描述" prop="description">
            <el-input v-model="from.description" placeholder="请填写系统描述" class="textarea" type="textarea" resize="none" rows="5"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button class="bottom_all_two" @click="resefrom()">取 消</el-button>
        <el-button class="bottom_all" @click="save()">确 定</el-button>
      </span>
    </el-dialog>
    <personModel :visible='visible' :isPerson="true" :choisedDatas="choisedData" @closeModel='closeModel' @getPerson='getPerson' />
  </div>
</template>

<script>
import breaDcrumb from "@/components/breadcrumb";
import personModel from '@/components/ChoisePerson/personModel';
import { mapGetters } from "vuex";
import {
  queryPage,
  saveOrUpdate,
  batchUpdateOrDelete,
  updateSortNum, Download,
} from "@/api/basedata/business.js";
import { getDicType } from "@/api/common.js";
import { codeReg, sortReg } from "@/common/RegExp.js";
export default {
  components: {
    breaDcrumb,
    personModel,
  },
  data() {
    var validatePass1 = (rule, value, callback) => {
      if (sortReg(value)) {
        callback();
      } else {
        callback(new Error("请输正整数序号!"));
      }
    };
    return {
      choisedData: [],
      DownloadURL: Download,
      rules: {
        name: [{ required: true, message: "请输入名称", trigger: "blur" }],
        code: [{ required: true, message: "请输入编码", trigger: "blur" }],
        userId: [
          { required: true, message: "请选择系统管理员", trigger: "change" },
        ],
        integrateWayId: [
          { required: true, message: "请选择集成方式", trigger: "change" },
        ],
        ssoAccount: [
          { required: true, message: "请输入SSO账号", trigger: "blur" },
        ],
        ssoPassword: [
          { required: true, message: "请输入SSO密码", trigger: "blur" },
        ],
        path: [{ required: true, message: "请输入口pash", trigger: "blur" }],
        typeId: [{ required: true, message: "请选择类型", trigger: "change" }],
        entrance: [
          { required: true, message: "请输入系统入口", trigger: "blur" },
        ],
        sortNum: [{ required: true, message: "请输入排序号", trigger: "blur" },
        { validator: validatePass1, trigger: "blur" }],
      },
      /*添加修改弹窗*/
      dialogVisible: false,
      dialogtitle: "添加系统",
      options: [],
      /*弹要修改过着添加的数据*/
      from: {
        name: "",
        code: "",
        description: "",
        enable: 1,
        entrance: "",
        integrateWayId: "",
        integrateWay: "",
        path: "",
        sortNum: "",
        ssoAccount: "",
        ssoPassword: "",
        typeId: "",
        typeName: "",
        userId: "",
        userName: "请选择",
      },
      /*判断收起展开*/
      showCardNav: true,
      /*搜索关键字段*/
      /*列表展示*/
      table: [
        { label: "系统名称", width: "auto", prop: "name", status: true },
        { label: "系统编码", width: "auto", prop: "code", status: true },
        { label: "系统管理员", width: "auto", prop: "userName", status: true },
        {
          label: "集成方式",
          width: "auto",
          prop: "integrateWay",
          status: true,
        },
        { label: "SSO账号", width: "auto", prop: "ssoAccount", status: true },
        { label: "SSO密码", width: "auto", prop: "ssoPassword", status: true },
        { label: "SSO URL", width: "auto", prop: "ssoUrl", status: true },
        { label: "入口pash", width: "auto", prop: "path", status: true },
        { label: "类型", width: "auto", prop: "typeName", status: true },
        { label: "系统入口", width: "auto", prop: "entrance", status: true },
        {
          label: "集成方式",
          width: "auto",
          prop: "integrateWay",
          status: true,
        },
        { label: "排序号", width: "auto", prop: "sortNum", status: true },
        { label: "系统描述", width: "auto", prop: "description", status: true },
        { label: "状态", width: "auto", prop: "", status: true },
        { label: "操作", width: "120", prop: "", status: true },
      ],
      /*列表选中的数据集*/
      tableParam: {
        page: 1,
        pageSize: 10,
        code: "",
        name: "",
      },
      total: 0,
      ids: [],
      sortIds: [],
      tableData: [],
      systemtype: [],
      systemintegrationmode: [],
      ButtonList: [],
      visible: false,
    };
  },
  mounted() {
    this.ButtonList = this.$ButtonList()
    getDicType({ code: "systemtype" }).then((res) => {
      this.systemtype = res.data.items;
    });
    getDicType({ code: "systemintegrationmode" }).then((res) => {
      this.systemintegrationmode = res.data.items;
    });
    this.queryPage();
  },
  methods: {
    closeModel () {
      this.visible = false;
    },
    handleClick () {
      this.visible = true;
      if (this.from.userId) {
        this.choisedData = JSON.parse(this.from.userId);
      }
    },
    getPerson (data) {
      this.from.userName = data.map(item => item.name).join(',');
      this.from.userId = JSON.stringify(data);
      this.visible = false;
    },
    changecode(key) {
      this.from[key] = codeReg(this.from[key]);
    },
    /*更新排序号*/
    updateSortNum() {
      const sorts = []
      this.tableData.forEach((v) => {
        sorts.push({ id: v.id, sortNum: v.sortNum })
      })
      updateSortNum(sorts).then((res) => {
        if (res.code === 1001) {
          this.$message.success('更新成功')
          this.queryPage();
        }
      });
    },
    /*重置*/
    reset() {
      this.tableParam.code = "";
      this.tableParam.name = "";
      this.queryPage();
    },
    /*删除，禁用，启用*/
    batchUpdateOrDelete(type, status) {
      if (this.ids.length == 0) {
        this.$message({
          message: "请选择要操作的数据项",
          type: "warning",
        });
        return;
      }
      if (type === "update_status") {
        this.$confirm("此操作将永久删除该信息?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          cancelButtonClass: "bottom_all_two",
          confirmButtonClass: "bottom_all",
          type: "warning",
        })
          .then(() => {
            batchUpdateOrDelete({
              ids: this.ids,
              status: status,
              type: type,
            }).then(() => {
              this.reset();
            });
          })
          .catch(() => { });
      } else {
        batchUpdateOrDelete({
          ids: this.ids,
          status: status,
          type: type,
        }).then(() => {
          this.queryPage();
        });
      }
    },
    /*保存或修改*/
    save() {
      this.$refs.dialogForm.validate((valid) => {
        if (valid) {
          let _this = this;
          _this.from.typeName = _this.systemtype.filter(function (item) {
            return item.id == _this.from.typeId;
          })[0].itemName;
          _this.from.integrateWay = _this.systemintegrationmode.filter(
            function (item) {
              return item.id == _this.from.integrateWayId;
            }
          )[0].itemName;
          saveOrUpdate(_this.from).then((res) => {
            if (res.code === 1001) {
              _this.resefrom();
              _this.queryPage();
              _this.$message.success('添加成功');
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resefrom() {
      this.dialogVisible = false;
      this.$refs.dialogForm.resetFields();
    },
    /*获取列表*/
    queryPage() {
      queryPage(this.tableParam).then((res) => {
        this.total = res.data.totalRows;
        this.tableData = res.data.data;
      });
    },
    add() {
      this.dialogtitle = "添加系统";
      this.$data.from = this.$options.data().from;
      console.log(this.from);
      setTimeout(() => {
        this.$refs.dialogForm.clearValidate();
      }, 10);
      this.dialogVisible = true;
    },
    update(data) {
      let str = JSON.stringify(data);
      this.from = JSON.parse(str);
      this.dialogtitle = "修改系统";
      this.dialogVisible = true;
      str = null;
    },
    /*批量选择*/
    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.ids = [];
      this.sortIds = [];
      val.forEach((item) => {
        this.ids.push(item.id);
        this.sortIds.push({ id: item.id, sortNum: item.sortNum });
      });
    },
    sizeChange(value) {
      this.tableParam.pageSize = value;
      this.queryPage();
    }
  },
  computed: {
    ...mapGetters({
      screenWidth: "getscreenWidth",
      screenHeight: "getscreenHeight",
      Collapse: "getsCollapse",
    }),
    style() {
      let num = 0;
      if (this.Collapse) {
        num = this.screenWidth - 124;
      } else {
        num = this.screenWidth - 220;
      }
      return "width:" + num + "px;";
    },
  },
};
</script>
<style lang="scss" scoped>
.dialoginfo {
  .el-input,
  .el-select {
    width: 200px;
  }
  .textarea {
    width: 500px;
  }
}
</style>
